﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Windows.Controls;
using System.Windows.Media.Media3D;
using System.Windows.Media;
using System.Windows;
using ARSS.Client;


namespace ARSS.Visual
{
    class VisualManager
    {
        #region Variables
        /// <summary>
        /// The visual presenter
        /// </summary>
        IPresenter thePresenter;

        /// <summary>
        /// "Host" window
        /// </summary>
        Window currentWindow;

        #endregion

        #region Functions

        /// <summary>
        /// Constructor
        /// </summary>
        public VisualManager()
        {
            this.thePresenter = null;
            this.currentWindow = null;
        }

        /// <summary>
        /// Start session
        /// </summary>
        public void StartPresentation()
        {
            //Subscribe to ar and camera update events
            Core.Instance.SubscribeCameraUpdateHandler(new Core.CameraUpdateHandler(OnCameraUpdate));
            Core.Instance.SubscribeARUpdateHandler(new Core.ARUpdateHandler(OnARUpdate));
        }

        /// <summary>
        /// End session
        /// </summary>
        public void StopPresentation()
        {
            Core.Instance.UnSubscribeCameraUpdateHandler(new Core.CameraUpdateHandler(OnCameraUpdate));
            Core.Instance.UnSubscribeARUpdateHandler(new Core.ARUpdateHandler(OnARUpdate));
        }

        /// <summary>
        /// Camera update event
        /// </summary>
        /// <param name="buffer">Video buffer</param>
        public void OnCameraUpdate(byte[] buffer)
        {
            try
            {
                this.thePresenter.OnCameraUpdate(buffer);
            }
            catch (Exception ex)
            {
                Logger.Instance.Log("VisualManager.onCamUpdate failed. Reason: " + ex.Message);
            }
        }

        /// <summary>
        /// AR update event
        /// </summary>
        /// <param name="markers"></param>
        public void OnARUpdate(ArrayList markers)
        {
            try
            {
                this.thePresenter.OnARUpdate(markers);
            }
            catch (Exception ex)
            {
                Logger.Instance.Log("VisualManager.onARUpdate failed. Reason: " + ex.Message);
            }
        }

        /// <summary>
        /// Align 3D camera
        /// </summary>
        /// <param name="_matrix">Projection matrix</param>
        public void AlignCamera(double[] matrix)
        {
            this.thePresenter.AlignCamera(matrix);
        }

        /// <summary>
        /// Change state
        /// </summary>
        /// <param name="state">Current State</param>
        public void ChangeState(Core.emSTATE state)
        {
            switch (state)
            {
                case Core.emSTATE.emINIT:
                    this.currentWindow = new MainWindow();
                    this.currentWindow.Show();
                    break;
                case Core.emSTATE.emMAIN:
                    if (this.currentWindow != null)
                        this.currentWindow.Close();

                    this.currentWindow = new PresentWindow();
                    this.thePresenter = ((PresentWindow)this.currentWindow);
                    this.currentWindow.Show();
                    
                    break;
            }
        }

        public SceneNode GetCollision(double[] matrix)
        {
            return this.thePresenter.GetCollision(matrix);
        }

        public void DisplayMsg(string message)
        {
            this.thePresenter.DisplayMsg(message);
        }


        #endregion
        
    }
}
